package com.settlement.system.service.impl;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.util.SecurityUtils;
import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblWpAuditArchiveRecordMapper;
import com.settlement.system.mapper.security.userdetails.SysUserDetails;
import com.settlement.system.model.entity.common.SystemUser;
import com.settlement.system.model.entity.common.TblWpAuditArchiveRecord;
import com.settlement.system.service.ITblWpAuditArchiveRecordService;
import com.settlement.system.service.ITblWpVendorConfigService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 稽核明细存档记录 服务实现类
 *
 * @author xiaochuan
 * @since 2022-07-04
 */
@Service
public class TblWpAuditArchiveRecordServiceImpl extends BaseServiceImpl implements ITblWpAuditArchiveRecordService {

    @Resource
    private TblWpAuditArchiveRecordMapper tblWpAuditArchiveRecordMapper;

    @Resource
    private ITblWpVendorConfigService tblWpVendorConfigService;

    @Override
    public BaseMapper init() {
        return tblWpAuditArchiveRecordMapper;
    }

    @Override
    public int selectCountUnCompleted() {
        return tblWpAuditArchiveRecordMapper.selectCountUnCompleted();
    }

    @Override
    public String archive(Map<String, Object> params) throws ServiceException {
        try {
            /*String message = checkIfCloseAccount(params);
            if(StringUtils.isNotEmpty(message)){
                return message;
            }
            unificationBillDetailApi.refreshNoMatch(params);*/
        } catch (Exception ex) {
            throw new ServiceException("存档异常", ex);
        }
        return null;
    }

    @Override
    public TblWpAuditArchiveRecord saveArchiveRecordBeforeQuery(Map<String, Object> params) throws ServiceException {
        SysUserDetails user = SecurityUtils.getUser();;
        String sendDate = params.get("sendDate") != null ? params.get("sendDate").toString() : "";
        String billDate = params.get("billDate") != null ? params.get("billDate").toString() : "";
        String rssDate = params.get("rssDate") != null ? params.get("rssDate").toString() : "";
        String sendInitDate = params.get("sendInitDate") != null ? params.get("sendInitDate").toString() : "\t";
        String billInitDate = params.get("billInitDate") != null ? params.get("billInitDate").toString() : "\t";
        String rssInitDate = params.get("rssInitDate") != null ? params.get("rssInitDate").toString() : "\t";
        String shopNos = params.get("shopNos") != null ? params.get("shopNos").toString() : "";

        TblWpAuditArchiveRecord record = new TblWpAuditArchiveRecord();
        record.setSendReturnDate((sendInitDate + "=" + sendDate).replace("-", "").replace("=", "-"));
        record.setBillDate((billInitDate + "=" + billDate).replace("-", "").replace("=", "-"));
        record.setReceiveSendStoreDate((rssInitDate + "=" + rssDate).replace("-", "").replace("=", "-"));
        String recordShopNos = sortShopNos(shopNos);
        record.setShopNos(recordShopNos);
        List<String> vendorCodeList = tblWpVendorConfigService.getVendorCodes(Arrays.asList(recordShopNos.split("\\/")));
        if (!CollectionUtils.isEmpty(vendorCodeList)) {
            String vendorCodes = vendorCodeList.toString().replaceAll("[\\[\\]\\s]", "");
            vendorCodes = vendorCodes.replaceAll(",", "/");
            record.setVendorCode(vendorCodes);
        }
        record.setId(UUID.randomUUID().toString().replace("-", ""));
        record.setCreateUser(user.getUsername());
        record.setCreateTime(new Date());
        insert(record);
        return record;
    }

    String sortShopNos(String shopNos) {
        String recordShopNos = shopNos//.replaceAll(",","/")
                .replaceAll("\\(", "")
                .replaceAll("\\)", "")
                .replaceAll("\\'", "");

        List<String> shopNoList = Arrays.asList(recordShopNos.split(","));
        shopNoList.sort(null);
        recordShopNos = shopNoList.toString().replaceAll("[\\[\\]\\s]", "");
        recordShopNos = recordShopNos.replaceAll(",", "/");
        return recordShopNos;
    }
}

